import logging

from celery import shared_task
from pymongo import MongoClient
from django.conf import settings

from .models import HelloModel

logger = logging.getLogger(__name__)


@shared_task
def hello_message(message):
    logger.info("Hello, %s!", message)
    return message


@shared_task
def add_hello(name):
    model = HelloModel(name=name)
    model.save()
    return


@shared_task
def insert_data_to_mongodb(message):
    client = MongoClient('mongodb://{}:{}@{}/?authSource={}'
                         .format(settings.MONGODB_USER,
                                 settings.MONGODB_PASSWORD,
                                 settings.MONGODB_SERVER,
                                 settings.MONGODB_DB))
    db = client[settings.MONGODB_DB]
    collection = db['hello']
    data = {"name": "John Doe", "age": 30, "city": "New York"}
    result = collection.insert_one(data)
    logger.info(f"Inserted document with id {result.inserted_id}")
    return
